package sbu.cs.ui.widget;

import java.util.HashMap;
import java.util.List;

import sbu.cs.IMessageObserver;
import sbu.cs.Settings;
import sbu.cs.ViewStack;
import sbu.cs.asdb.ASDBUtils;
import sbu.cs.ui.widget.UserInputView.UserInputViewEventListener;

import android.content.Context;
import android.os.Handler;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MainMenu extends RelativeLayout implements OnClickListener {

	private MainMenuItem sendMenuItem;
	private MainMenuItem receiveMenuItem;
	private MainMenuItem clearMessages;
	private MainMenuItem clearLastMessage;

	private UserInputView userInputView;
	private MessagesView messagesView;
	private ViewStack viewStack = ViewStack.createViewStack();

	private static final int ID_ITEM_SEND = 200;
	private static final int ID_ITEM_RECEIVE = 201;
	private static final int ID_ITEM_CLEAR_MESSAGES = 202;
	private static final int ID_ITEM_CLEAR_LAST_MESSAGE = 203;

	protected static final String LOG_TAG = "MainMenu";

	public Handler mHandler = new Handler();
	public Runnable mRunnable = new Runnable() {
		public void run() {
			List<HashMap<String, String>> res = ASDBUtils.createASDBUtils()
					.selectData();
			if (res.size() > 0) {
				messagesView.setMessagesList(res);
				sendMenuItem.setVisibility(View.INVISIBLE);
				receiveMenuItem.setVisibility(View.INVISIBLE);
				clearMessages.setVisibility(View.INVISIBLE);
				clearLastMessage.setVisibility(View.INVISIBLE);
				messagesView.setVisibility(VISIBLE);
				viewStack.pushView(messagesView);
			} else {
				Toast t = Toast.makeText(getContext(),
						"No vircasts at this location!", Toast.LENGTH_SHORT);
				t.show();
			}
		}
	};

	public MainMenu(Context context, String text1, String text2, String text4,
			String text3) {
		super(context);
		init(text1, text2, text3, text4, context);
	}

	private void init(String text1, String text2, String text3, String text4,
			Context context) {
		sendMenuItem = new MainMenuItem(context, text1);
		RelativeLayout.LayoutParams i1param = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 50);
		i1param.topMargin = 70;
		sendMenuItem.setId(ID_ITEM_SEND);
		addView(sendMenuItem, i1param);
		sendMenuItem.setOnClickListener(this);

		receiveMenuItem = new MainMenuItem(context, text2);
		RelativeLayout.LayoutParams i2param = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 50);
		// i2param.leftMargin = 4;
		i2param.topMargin = 40;
		i2param.addRule(BELOW, sendMenuItem.getId());
		receiveMenuItem.setId(ID_ITEM_RECEIVE);
		receiveMenuItem.setOnClickListener(this);
		addView(receiveMenuItem, i2param);

		clearMessages = new MainMenuItem(context, text3);
		RelativeLayout.LayoutParams i3param = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 50);
		i3param.topMargin = 40;
		i3param.addRule(BELOW, receiveMenuItem.getId());
		clearMessages.setId(ID_ITEM_CLEAR_MESSAGES);
		clearMessages.setOnClickListener(this);
		addView(clearMessages, i3param);

		clearLastMessage = new MainMenuItem(context, text4);
		RelativeLayout.LayoutParams i4param = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 50);
		i4param.topMargin = 40;
		// i4param.leftMargin = 4;
		i4param.addRule(BELOW, clearMessages.getId());
		// i4param.addRule(RIGHT_OF, clearMessages.getId());
		clearLastMessage.setId(ID_ITEM_CLEAR_LAST_MESSAGE);
		clearLastMessage.setOnClickListener(this);
		addView(clearLastMessage, i4param);

		userInputView = new UserInputView(context);
		userInputView.setEventListener(new UserInputViewEventListener() {
			public void onEvent(int event) {
				switch (event) {
				case UserInputView.EVENT_SUBMIT_BUTTON_PRESSED:
					// win.dismiss();
					sendMenuItem.setVisibility(View.VISIBLE);
					receiveMenuItem.setVisibility(View.VISIBLE);
					clearMessages.setVisibility(View.VISIBLE);
					clearLastMessage.setVisibility(View.VISIBLE);
					userInputView.setVisibility(View.INVISIBLE);
					viewStack.popView();

					if (Settings.getPreferenceManager(null).getLatitude() == 0
							&& Settings.getPreferenceManager(null)
									.getLongitude() == 0) {
						Toast t = Toast.makeText(getContext(),
								"Location not set. Vircast unseccessful.",
								Toast.LENGTH_SHORT);
						t.show();
					} else {
						// userInputView.getCategory()
						// TODO
						// ASDBUtils.createASDBUtils().deleteAllData();
						ASDBUtils.createASDBUtils().populateData(
								userInputView.getMessage(),
								userInputView.getCategory(),
								userInputView.getRadius().toString(),
								userInputView.getPriority(),
								userInputView.getSubject(),
								userInputView.getTimeoutVal().toString());
					}
					break;
				}
			}
		});
		userInputView.setVisibility(View.INVISIBLE);
		addView(userInputView);

		messagesView = new MessagesView(context);
		messagesView.setVisibility(INVISIBLE);
		addView(messagesView);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case ID_ITEM_SEND:
			sendMenuItem.setVisibility(View.INVISIBLE);
			receiveMenuItem.setVisibility(View.INVISIBLE);
			clearMessages.setVisibility(View.INVISIBLE);
			clearLastMessage.setVisibility(View.INVISIBLE);
			userInputView.setVisibility(View.VISIBLE);
			viewStack.pushView(userInputView);
			break;
		case ID_ITEM_RECEIVE:
			mHandler.post(mRunnable);
			break;
		case ID_ITEM_CLEAR_MESSAGES:
			ASDBUtils.createASDBUtils().deleteAllData();
			Toast t = Toast.makeText(getContext(),
					"Your Vircasts have been deleted", Toast.LENGTH_SHORT);
			t.show();
			break;
		case ID_ITEM_CLEAR_LAST_MESSAGE:
			if (Settings.getPreferenceManager(null).getLastMessage().equals(
					Settings.DEF_LAST_MESSAGE)) {
				Toast t1 = Toast.makeText(getContext(), "Nothing to Undo",
						Toast.LENGTH_SHORT);
				t1.show();
			} else {
				ASDBUtils.createASDBUtils().deleteLastMessage();
				Toast t1 = Toast.makeText(getContext(),
						"Last Vircast deleted", Toast.LENGTH_SHORT);
				t1.show();
			}
			break;
		}
	}

	public boolean onBackPressed() {
		if (viewStack.currentView().equals(messagesView)) {
			sendMenuItem.setVisibility(View.VISIBLE);
			receiveMenuItem.setVisibility(View.VISIBLE);
			clearMessages.setVisibility(View.VISIBLE);
			clearLastMessage.setVisibility(View.VISIBLE);
			messagesView.setVisibility(INVISIBLE);
			viewStack.popView();
			return true;
		} else if (viewStack.currentView().equals(userInputView)) {
			sendMenuItem.setVisibility(View.VISIBLE);
			receiveMenuItem.setVisibility(View.VISIBLE);
			clearMessages.setVisibility(View.VISIBLE);
			clearLastMessage.setVisibility(View.VISIBLE);
			userInputView.setVisibility(View.INVISIBLE);
			viewStack.popView();
			return true;
		}
		return false;
	}

	public void setMessageObserver(IMessageObserver observer) {
		messagesView.setMessageObserver(observer);
	}
}
